package ngat.oss;

import ngat.message.OSS.*;
import ngat.util.logging.*;
import ngat.phase2.*; 
import ngat.instrument.*;
import ngat.phase2.nonpersist.*;

import java.util.*;
import java.io.*;

import jyd.dbms.*;
import jyd.storable.*;
import jyd.collection.*;

/**
 * Action: Sent by USR to remove a CoI User from a Proposal.
 * @author S.N.Fraser
 * $Id: DELETE_COI_USERImpl.java,v 1.1 2006/11/27 08:45:00 snf Exp $
 */
public class DELETE_COI_USERImpl extends TransactionImpl {
    
    /** The path used to locate the Proposal in the OSS database. */
    protected Path proposalPath;
    
    /** The Path for the user who is being added as CoI.*/
    protected String userId;
  
    /** Create a DELETE_COI_USERImpl for the supplied request.*/
    public DELETE_COI_USERImpl(DELETE_COI_USER request) {
	super(request);
	this.proposalPath = request.getProposalPath();
	this.userId       = request.getUserId();
    }
    
    /**
     *Removes the specified Proposal from the specified User's CoI list.
     * @return Boolean to indicate success.
     */
    public TRANSACTION_DONE exec() { 

	Transaction t = null;
	try {
	    t = Transaction.current();
	    
	    Root root = OSS_Utilities.fetchRoot();
	    Tag  tag  = null;  
	    User user = null;
	    User searchUser = null;
	    
	    Iterator it1 = root.listAllTags();
	    while (it1.hasNext()) {
		tag = (Tag)it1.next();
		Iterator it2 = tag.listAllUsers();
		while (it2.hasNext()) {
		    user = (User)it2.next();
		    if (user.getName().equals(userId)) {
			searchUser = user;
		    }
		}
	    }
	    
	    // Check it exists.
	    if (searchUser == null) {
		logger.log(1, "DELETE_COI_USERImpl", id, "exec", 
			   "User: "+userId+" Not in database.");
		return error(TRANSACTION.PATH_RESOLUTION_ERROR, 
				     "Could not find User: "+userId);
	    }			
	    
	    logger.log(1, "DELETE_COI_USERImpl", id, "exec", " Found user: "+userId+" ("+
		       searchUser.getFname()+" "+searchUser.getSname()+
		       " email: "+ searchUser.getEmail());
	    
	    // Fetch the Proposal.
	    Proposal proposal = null;
	    
	    try {
		proposal = OSS_Utilities.fetchProposal(proposalPath);
	    } catch (IOException iox) {
		logger.log(1, CLASS, id, "exec",
			   "Proposal: "+proposalPath.toString()+
			   " Error fetching from Database: "+iox);
		return error(TRANSACTION.DATABASE_READ_ERROR,			   
				     "Proposal: "+proposalPath.toString()+
				     " Error reading from database: "+iox);
	    }		

	    // Check the Proposal exists.
	    if (proposal == null) {
		logger.log(1, "DELETE_COI_USERImpl", id, "exec", 
			   "Proposal: "+proposalPath.toString()+" Not in database.");
		return error(TRANSACTION.PATH_RESOLUTION_ERROR, 
				     "Could not find Proposal: "+proposalPath.toString());
	    } 
	    
	    // See if USer is CoI for this Proposal.
	    if (searchUser.findCoIProposal(proposal.getName()) == null) {
		logger.log(1, "DELETE_COI_USERImpl", id, "exec", " User: "+userId+
			   " is not CoI for Proposal: "+proposalPath);
		// TR failed but intent is correct.
		
		DELETE_COI_USER_DONE done = new DELETE_COI_USER_DONE(id);
		done.setRemoved(false);
		return done;
	     
	    }
	    
	    // Delte the link.
	    searchUser.deleteCoIProposal(proposal.getName());
	    logger.log(1, "DELETE_COI_USERImpl", id, "exec", 
		       "Deleted User: "+userId);
	    
	    // Check it worked.
	    if (searchUser.findCoIProposal(proposal.getName()) != null) {
		logger.log(1, "DELETE_COI_USERImpl", id, "exec", 
			   "User: "+userId+
			   " was not added as CoI for Proposal: "+proposalPath);
		return error(TRANSACTION.DATABASE_UPDATE_ERROR,
				     "User could not be removed as CoI");
	    }
	   
	    DELETE_COI_USER_DONE done = new DELETE_COI_USER_DONE(id); 
	    done.setSuccessful(true);
	    done.setRemoved(true);
	    return done;

	} finally {
	    t.commit();
	}
    }
    
}

/** $Log: DELETE_COI_USERImpl.java,v $
/** Revision 1.1  2006/11/27 08:45:00  snf
/** Initial revision
/**
/** Revision 1.1  2001/02/23 18:51:14  snf
/** Initial revision
/** */
